<%-- 
    Document   : index
    Created on : 18-nov-2013, 11.35.34
    Author     : Ilo
--%>

<%@page import="java.util.LinkedList"%>
<%@page import="it.evento.Events"%>
<%@page import="it.evento.EventPrintable"%>
<%@page import="java.util.List"%>
<%@page import="it.evento.utils.genpage.Article"%>
<%@page import="it.evento.utils.genpage.ArticleRow"%>
<%@page import="it.evento.utils.genpage.RowCell"%>
<%@page import="it.evento.Users"%>
<%@page import="it.evento.utils.genpage.BlogPost"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%
    boolean isLoggedUser = false;
    Users loggedUser = null;
    
    if(session.getAttribute("loggedUser")!=null) {
        loggedUser = (Users)session.getAttribute("loggedUser");
        isLoggedUser = true;
    }
    else {
        isLoggedUser = false;
    }
%> 

<!DOCTYPE html>
<html dir="ltr" lang="it-IT">
    <head>
        <title>EvenTO</title>

        <jsp:include page="html/metadata.jsp" flush="true" />
        <script>
            $(document).ready(function() {
                RATY_DEFAULT_OPTIONS = {
                                        path: 'images/raty/',
                                        half: false,
                                        space: false,
                                        hints: ['Brutto', 'Scarso', 'Normale', 'Buono', 'Ottimo'],
                                        readOnly: <%= isLoggedUser ? false : true %>,
                                        score: function() {
                                          return $(this).attr('data-score');
                                        },
                                        click: manageStarsClick
                                       };
                
                /* Load last events */
                $.post("Controller",
                       {type: "loadLastEvents"},
                       function(data) {
                           var parent = $('#artLastEventsContent').parent();
                           $('#artLastEventsContent').remove();
                           
                           eventsDom = $('<output>').append($.parseHTML(data.message));                   
                           $('[id^=starsfor]', eventsDom).raty(RATY_DEFAULT_OPTIONS);
                           parent.empty();
                           parent.append(eventsDom);
                       },
                       'json');
                /* /Load last events */

                <% if(isLoggedUser) { %>
                    /* Load favourite events */
                    $.post("Controller",
                           {type: "loadFavouriteEvents",
                            userid: <%= loggedUser.getId() %>},
                            function(data) {
                                var parent = $('#artFavouriteEventsContent').parent();
                                $('#artFavouriteEventsContent').remove();
                           
                                eventsDom = $('<output>').append($.parseHTML(data.message));                   
                                $('[id^=starsfor]', eventsDom).raty(RATY_DEFAULT_OPTIONS);
                                parent.empty();
                                parent.append(eventsDom);
                            },
                            'json');
                    /* /Load favourite events */
                <% } %>
                
                /* Manage Stars */
                function manageStarsClick(score, evt) {
                    var self = $(this);
                    var eid = this.id.split("-")[1];
                    var votes = parseInt($('#votesfor-'+eid).html().split(" ")[0]);
                    var oldScore = self.attr('data-score');
                    
                    $.post("Controller",
                           {type: "eventStarsRate",
                            eventid: eid,
                            score: score},
                           function(data) {                               
                               if(data.status === 0) {
                                   votes++;
                                   self.raty('score', data.score).attr('data-score', data.score);
                                   $('#votesfor-'+eid).html(votes +  " vot" + (votes===1 ? "o" : "i") + ".");
                               }
                               else {
                                   self.raty('score', oldScore);
                                   $('#modal-window .modal-title').html("ERRORE");
                                   $('#modal-window #modal-message').html(data.message);
                                   $('#modal-window').modal('show');
                               }
                           },
                           'json');
                }
                /* /Manage Stars */
            });
        </script>
    </head>
    <body>
        <div id="evento-main">
            <!-- header -->
            <jsp:include page="html/header.jsp" flush="true" />
            <!-- /header -->

            <!-- top-menu -->
            <%@include file="html/top-menu.jsp" %>
            <!-- /top-menu -->

            <div class="evento-sheet clearfix">
                <div class="evento-layout-wrapper">
                    <div class="evento-content-layout">
                        <div class="evento-content-layout-row">
                            <div class="evento-layout-cell evento-content">
                                <%=
                                    new BlogPost()
                                            .setTitle("Benvenuto!")
                                            .setContent("<h3>Benvenuto su EvenTO!</h3>"
                                                      + "<p>Qui puoi scoprire tutti gli ultimi eventi in zona!</p>"
                                                      + "<p>Pianificare la tua serata!</p>"
                                                      + "<p>Condividere il tuo programma con gli amici di <b>Facebook</b> e <b>Google+</b></p>"
                                                      + "<br>"
                                                      + "<h4>Divertiti!!!</h4>")
                                            .print()
                                %>
                                
                                <% if(isLoggedUser) { %>
                                    <%=
                                    new Article()
                                            .setTitle("I tuoi eventi preferiti")
                                            .setContent("<div id='artFavouriteEventsContent' style='text-align:center'><img src='images/preloader_green.gif' /></div>")
                                            .print()
                                    %>
                                <% } %>
                                
                                <%=
                                new Article()
                                        .setTitle("Gli ultimi eventi")
                                        .setContent("<div id='artLastEventsContent' style='text-align:center'><img src='images/preloader_green.gif' /></div>")
                                        .print()
                                %>

                            </div>

                            <!-- sidebar -->
                            <jsp:include page="html/sidebar.jsp" />
                            <!-- /sidebar -->
                            
                        </div>
                    </div>
                </div>

                <!-- footer -->
                <%@include file="html/footer.jsp" %>
                <!-- /footer -->

            </div>
        </div>
        
        <!-- modal windows -->
        <%@include file="html/modal.jsp" %>
        <!-- /modal windows -->
    </body>
</html>
